<?php

class UserConnectionsTb extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var integer
     */
    public $friend_id;

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var string
     */
    public $date_created;

    /**
     * Independent Column Mapping.
     */

   
    public function beforeValidationOnCreate()
    {
        $this->date_created = CURR_DATE;
    }

    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'friend_id' => 'friend_id', 
            'user_id' => 'user_id', 
            'date_created' => 'date_created'
        );
    }
    public function listConnection($user_id = '')
    {
        $phql = "SELECT uat.*,uct.* FROM UserConnectionsTb uct LEFT JOIN UserAccountTb uat ON uat.id = uct.friend_id
               WHERE uct.user_id = ?0";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return $data;
    }

    public function listOnline($user_id)
    {
        $phql = "SELECT uat.*, uct.* FROM UserConnectionsTb uct
                 LEFT JOIN UserAccountTb uat ON uat.id = uct.friend_id
                 WHERE uct.user_id = ?0";
        $data = $this->modelsManager->executeQuery($phql,array(0=>$user_id));
        return $data;
    }

    public function listPossibleConnection($user_id)
    {  
        // GET LIST OF FRIENDS
        $phql1 = "SELECT friend_id FROM UserConnectionsTb WHERE user_id = ?0";
        $friends_list = $this->modelsManager->executeQuery($phql1,array(0=>$user_id));
        $friends_id = array();
        foreach ($friends_list as $value) {
            $friends_id[] = $value->friend_id;
        }
        // GET LIST OF ID OF PENDING REQUEST
        $phql2 = "SELECT receiver_id FROM ConnectionRequestTb WHERE sender_id = ?0";
        $pending_list = $this->modelsManager->executeQuery($phql2,array(0=>$user_id));
        foreach ($pending_list as $value){
            $friends_id[] =  $value->receiver_id;
        }

        $data_string = implode(",", $friends_id);

        // GET LIST OF POSSIBLE CONNECTION
        $condition = "";
        if($data_string)  $condition = " AND id NOT IN (" . $data_string . ")";
        $phql3 = "SELECT uat.id,uat.fname,uat.mname,uat.lname,uat.profile_pic FROM UserAccountTb uat WHERE id != ?0".$condition;
        $result = $this->modelsManager->executeQuery($phql3,array(0=>$user_id));

        return $result;
    }

    public function checkIfConnected($my_id,$other_id)
    {
        $connection = UserConnectionsTb::findFirst(array(
                            "conditions"=>"user_id = ?0 and friend_id = ?1",
                            "bind"=>array(0=>$my_id,1=>$other_id)
                        ));
        return $connection;
    }

    public function insertConnection($my_id,$other_id)
    {
        $this->db->begin();

        $connection = new UserConnectionsTb();
        $connection->user_id = $my_id;
        $connection->friend_id = $other_id;
        if($connection->save() == false)
        {
            $this->db->rollback();    
            return false;
        }

        $connection = new UserConnectionsTb();
        $connection->user_id = $other_id;
        $connection->friend_id = $my_id;
        if($connection->save() == false)
        {
            $this->db->rollback();    
            return false;
        }        
            $this->db->commit();
            return true;

    }

}
